package com.android.baselibrary.utils

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Point
import android.graphics.Rect
import android.view.*
import androidx.activity.ComponentActivity
import androidx.coordinatorlayout.widget.ViewGroupUtils
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.android.baselibrary.app.MainApplication

/**
 * 扩展点击区域
 */
@SuppressLint("RestrictedApi")
fun View.expand(dx: Int, dy: Int) {
    class MultiTouchDelegate(bound: Rect? = null, delegateView: View) : TouchDelegate(bound, delegateView) {
        val delegateViewMap = mutableMapOf<View, Rect>()
        private var delegateView: View? = null

        override fun onTouchEvent(event: MotionEvent): Boolean {
            val x = event.x.toInt()
            val y = event.y.toInt()
            var handled = false
            when (event.actionMasked) {
                MotionEvent.ACTION_DOWN -> {
                    delegateView = findDelegateViewUnder(x, y)
                }
                MotionEvent.ACTION_CANCEL -> {
                    delegateView = null
                }
            }
            delegateView?.let {
                event.setLocation(it.width / 2f, it.height / 2f)
                handled = it.dispatchTouchEvent(event)
            }
            return handled
        }

        private fun findDelegateViewUnder(x: Int, y: Int): View? {
            delegateViewMap.forEach { entry -> if (entry.value.contains(x, y)) return entry.key }
            return null
        }
    }

    val parentView = parent as? ViewGroup
    parentView ?: return

    if (parentView.touchDelegate == null) parentView.touchDelegate = MultiTouchDelegate(delegateView = this)
    post {
        val rect = Rect()
        ViewGroupUtils.getDescendantRect(parentView, this, rect)
        rect.inset(- dx, - dy)
        (parentView.touchDelegate as? MultiTouchDelegate)?.delegateViewMap?.put(this, rect)
    }


}
fun <T : ViewModel> Fragment.getModel(modelClass: Class<T>): T {
    return ViewModelProvider(this)[modelClass]
}
fun <T : ViewModel> FragmentActivity.getModel(modelClass: Class<T>): T {
    return ViewModelProvider(this)[modelClass]
}
fun getDurationString(duration: Long): String? {
    val days = duration / (1000 * 60 * 60 * 24)
    val hours = duration % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)
    val minutes = duration % (1000 * 60 * 60) / (1000 * 60)
    val seconds = duration % (1000 * 60) / 1000
    val hourStr = if (hours < 10) "0$hours" else hours.toString() + ""
    val minuteStr = if (minutes < 10) "0$minutes" else minutes.toString() + ""
    val secondStr = if (seconds < 10) "0$seconds" else seconds.toString() + ""
    return if (hours != 0L) {
        "$hourStr:$minuteStr:$secondStr"
    } else {
        "$minuteStr:$secondStr"
    }


}
fun px2dp(context: Context,px:Int):Int = ((px-0.5)/context.resources.displayMetrics.density).toInt()
fun dp2px(context: Context,dp:Int): Int = (context.resources.displayMetrics.density*dp+0.5).toInt()
fun dp2px(dp:Int): Int = (MainApplication.mainApplication.resources.displayMetrics.density*dp+0.5).toInt()
fun px2dp(px:Int):Int = ((px-0.5)/MainApplication.mainApplication.resources.displayMetrics.density).toInt()
fun getScreenWidth(): Int {
    val wm: WindowManager = MainApplication.Companion.mainApplication.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        ?: return -1
    val point = Point()
    wm.defaultDisplay.getSize(point)
    return point.x
}
fun getScreenHeight(): Int {
    val wm: WindowManager = MainApplication.Companion.mainApplication.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        ?: return -1
    val point = Point()
    wm.defaultDisplay.getSize(point)
    return point.y
}
fun <T:ViewModel> ComponentActivity.getViewModelOfActivity(modelClass: Class<T>):T{
    return ViewModelProvider(this,ViewModelProvider.NewInstanceFactory())[modelClass]
}
fun <T:ViewModel> Fragment.getViewModelOfFragment(modelClass: Class<T>):T{
    return ViewModelProvider(this,ViewModelProvider.NewInstanceFactory())[modelClass]
}